package com.coin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.*;
import com.coin.dto.UserBankDto;
import com.coin.mapper.UserBankMapper;
import com.coin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 李力
 * @description 针对表【user_bank(用户人民币提现地址)】的数据库操作Service实现
 * @createDate 2025-06-07 10:16:53
 */
@Service
public class UserBankServiceImpl extends ServiceImpl<UserBankMapper, UserBank>
        implements UserBankService {

    @Autowired
    private UserService userService;

    /**
     * @Description: 分页查询用户的银行卡
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @Override
    public Page<UserBank> findByPage(Page<UserBank> page, Long usrId) {
        LambdaQueryWrapper<UserBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(usrId != null, UserBank::getId, usrId);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 修改银行卡的状态
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, Byte status) {
        if (id == null || status == null) {
            throw new RuntimeException("参数为空");
        }
        UserBank userBank = new UserBank();
        userBank.setId(id);
        userBank.setStatus(status);
        return updateById(userBank);
    }

    /**
     * @Description: 修改银行卡
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBank(UserBank userBank) {
        if (userBank == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(userBank);
    }

    /**
     * @Description: 绑定银行卡
     * @Author: 李 力
     * @Date: 2025/6/11
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindBank(String userId, UserBank userBank) {
        if (userId == null || userBank == null) {
            throw new RuntimeException("参数为空");
        }
        User user = userService.getById(Long.valueOf(userId));
        if (user == null) {
            throw new RuntimeException("无此用户");
        }
        if (user.getId().toString().equals(userBank.getUserId().toString())) {
            throw new RuntimeException("用户信息不匹配");
        }
        LambdaQueryWrapper<UserBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(StrUtil.isNotEmpty(userBank.getBankCard()), UserBank::getBankCard, userBank.getBankCard())
                .eq(UserBank::getUserId, userId);
        long count = count(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("该用户已经绑定此银行卡");
        }
        String password = user.getPassword();
        String payPassword = userBank.getPayPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(payPassword, password);
        if (!matches) {
            throw new RuntimeException("支付密码错误");
        }
        return save(userBank);
    }

    @Override
    public UserBankDto getUserBankInfo(Long userId) {
        if (userId == null) {
            throw new RuntimeException("参数为空");
        }
        LambdaQueryWrapper<UserBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(UserBank::getUserId, userId);
        List<UserBank> userBank = list(queryWrapper);
        if (userBank == null || userBank.isEmpty()) {
            throw new RuntimeException("无此用户");
        }
        UserBankDto t = new UserBankDto();
        BeanUtil.copyProperties(userBank.get(0), t);
        return t;
    }
}




